package com.mm.base.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.mm.util.common.SpringContextUtil;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

/**
 * swagger配置变化原因：
 * Swagger版本
 * 1、Swagger 2.x 版本
 * springboot 2.5.6 ✚ swagger 2.6.1~2.9.2配置属下
 * 2、Swagger 3.x 版本
 * springboot 2.5.6 ✚ swagger 3.0.0配置属下
 * springboot 2.6.5 ✚ swagger 3.0.0配置属下
 * Knife4j 版本
 * Knife4j 3.0.3 ✚ Springboot2.7.1
 *
 * @author yl
 */
@Slf4j
@Configuration
@EnableOpenApi
// Knife4j增强
@EnableKnife4j
public class Swagger3Config {

    @Value("${server.port}")
    private int port;
    @Value("${server.servlet.context-path:}")
    private String contextPath;

    @Bean
    @SneakyThrows
    public Docket createApi() {
        Docket docket = new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .globalRequestParameters(getGlobalRequestParameters())
                // 设置自定义返回消息体
                .globalResponses(HttpMethod.GET, globalResponse())
                .globalResponses(HttpMethod.POST, globalResponse())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // .apis(RequestHandlerSelectors.basePackage("com.mm.controller"))
                .paths(PathSelectors.any())
                .build()
                .groupName("Alone");
        return docket;
    }

    /**
     * API接口说明
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("木木打车")
                .description("1.0.0001版本【接口文档接口】")
                .termsOfServiceUrl("https://commandfight/api")
                .contact(new Contact("JueDream", "https://gitee.com/yling_dream", "yling_mumu@163.com"))
                .version("1.0.0001")
                .build();
    }

    /**
     * 生成全局通用参数
     * java.util.List<springfox.documentation.service.RequestParameter>
     *
     * @return
     */
    private List<RequestParameter> getGlobalRequestParameters() {
        List<RequestParameter> parameters = new ArrayList<>();

        //设置请求头
        parameters.add(new RequestParameterBuilder()
                .name("user_code")
                .description("当前登录用户编码【开发自测时使用】")
                .required(false)
                .in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .required(false)
                .build());
        return parameters;
    }

    /**
     * globalResponse
     *
     * @return
     */
    private List<Response> globalResponse() {
        List<Response> responseList = new ArrayList<>();
        responseList.add(new ResponseBuilder().code("400").description("错误请求").build());
        responseList.add(new ResponseBuilder().code("401").description("未认证").build());
        responseList.add(new ResponseBuilder().code("403").description("请求被禁止").build());
        responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
        responseList.add(new ResponseBuilder().code("500").description("服务器内部错误").build());
        return responseList;
    }
}
